<?php include("include/db.class.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--<meta http-equiv="Content-Type" content="multipart/x-mixed-replace;boundary=--ThisRandomString--; charset=utf-8" />-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
session_start();
if(!$_SESSION['login_status']){
	echo '非法访问！';
	exit();
}

$user = $_GET['user'];
$sql = "SELECT from_id,to_id,msg,date_time FROM vv_posts where from_id='".$user."' or to_id='".$user."'";
$result = $DB->query($sql);
$COUNT = 0;//$DB->affected_rows();
set_time_limit(60);
$i = 0;
while(1){
	if(!connection_aborted()){
		$i++;
		$count = $DB->affected_rows();
		if($COUNT != $count){
			for($n = $COUNT; $n < $count; $n++){
				$DB->seek($n);
				$array = mysql_fetch_array($result);
				push('{from:"'.$array['from_id'].'",to:"'.$array['to_id'].'",msg:"'.$array['msg'].'"}');	
			}
			$COUNT = $count;
		}
		flush();
		ob_flush();
		$DB->free();
		$result = $DB->query($sql);
		sleep(0.5);
	}else{
        exit();
	}
}
/*register_shutdown_function('log_it');

function log_it(){
	$endtime = date('h:i:s');
    $status = 'seconds:'."  connection status:".connection_status()."  connection_aborted:".connection_aborted();
    file_put_contents('stop.txt',"$starttime -> $endtime : stop  $status");
	exit();
}*/

function push($txt){
	echo '<script type="text/javascript">window.parent.comet.display('.$txt.');</script>';
}

?>
</body>
</html>
